package org.jeecg.trace.controller;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.smallbun.screw.core.util.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.client.BizResponse;
import org.jeecg.client.SendUtils;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.CudUtils;
import org.jeecg.common.util.oConvertUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.jeecg.trace.entity.QisReinspectValue;
import org.jeecg.trace.service.IQisReinspectValueService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 不良录入明细
 * @Author: LiCY
 * @Date:   2023-11-28
 * @Version: V1.0
 */
@Api(tags="不良录入明细")
@RestController
@RequestMapping("/trace/qisReinspectValue")
@Slf4j
public class QisReinspectValueController extends JeecgController<QisReinspectValue, IQisReinspectValueService> {
	@Autowired
	private IQisReinspectValueService qisReinspectValueService;
	@Autowired
	private CommonAPI commonAPI;
	/**
	 * 分页列表查询666
	 *
	 * @param param
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "不良录入明细-分页列表查询")
	@ApiOperation(value="不良录入明细-分页列表查询", notes="不良录入明细-分页列表查询")
	@GetMapping(value = "/list")
	@DS("#header.his")
	public Result<IPage<QisReinspectValue>> queryPageList(@RequestParam Map<String, Object> param,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		Page<QisReinspectValue> page = new Page<QisReinspectValue>(pageNo, pageSize);
		IPage<QisReinspectValue> pageList = qisReinspectValueService.pageList(page,param);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param qisReinspectValue
	 * @return
	 */
	@AutoLog(value = "不良录入明细-添加")
	@ApiOperation(value="不良录入明细-添加", notes="不良录入明细-添加")
	//@RequiresPermissions("org.jeecg.modules.demo:qis_reinspect_value:add")
	@PostMapping(value = "/add")
	@DS("#header.line")
	public Result<String> add(@RequestBody QisReinspectValue qisReinspectValue) throws Exception {
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String formattedDate = sdf.format(now);
		if (qisReinspectValue.getNum()>=1){
			Map<String, String> getcounter = CudUtils.getCudMap("getcounter", "recid.qis_reinspect_value;" + qisReinspectValue.getNum() + ";0", qisReinspectValue.getLine(), qisReinspectValue);
//			Integer counterValue = commonAPI.getCounterValue("recid.qis_reinspect_value_err");
			BizResponse bizResponse = SendUtils.sendRunCmd(getcounter);
			int returnMesssage = Integer.parseInt(bizResponse.getReturnMesssage());
			int z = returnMesssage+qisReinspectValue.getNum();
			for (int i =returnMesssage;i<z;i++){
				LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
				String num = String.format("%04d", commonAPI.getCounterValue("num.qis_reinspect_value","0"));
				qisReinspectValue.setUnitsn("E"+formattedDate+num);
				qisReinspectValue.setRecId(i);
				qisReinspectValue.setUser(sysUser.getUsername());
				qisReinspectValue.setStatus("0");
				qisReinspectValueService.save(qisReinspectValue);
				System.out.println(i);
			}
		}
		return Result.OK("添加成功！");
	}

	 /**
	  *   添加
	  *
	  * @param qisReinspectValue
	  * @return
	  */
	 @AutoLog(value = "不良录入明细-添加")
	 @ApiOperation(value="不良录入明细-添加", notes="不良录入明细-添加")
	 //@RequiresPermissions("org.jeecg.modules.demo:qis_reinspect_value:add")
	 @PostMapping(value = "/addValue")
	 @DS("#header.line")
	 public Result<String> addValue(@RequestBody QisReinspectValue qisReinspectValue) {
		 Integer counterValue = commonAPI.getCounterValue("recid.qis_reinspect_value_err","0");
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 qisReinspectValue.setRecId(counterValue);
		 qisReinspectValue.setUser(sysUser.getUsername());
		 qisReinspectValueService.save(qisReinspectValue);
		 return Result.OK("添加成功！");
	 }
	
	/**
	 *  编辑
	 *
	 * @param qisReinspectValue
	 * @return
	 */
	@AutoLog(value = "不良录入明细-编辑")
	@ApiOperation(value="不良录入明细-编辑", notes="不良录入明细-编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:qis_reinspect_value:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	@DS("#header.line")
	public Result<String> edit(@RequestBody QisReinspectValue qisReinspectValue) {
		qisReinspectValueService.updateById(qisReinspectValue);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过选中对象删除
	 *
	 * @param qisReinspectValue
	 * @return
	 */
	@AutoLog(value = "不良录入明细-通过id删除")
	@ApiOperation(value="不良录入明细-通过id删除", notes="不良录入明细-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.demo:qis_reinspect_value:delete")
	@RequestMapping(value = "/delete")
	@DS("#header.line")
	public Result<String> delete(@RequestBody QisReinspectValue qisReinspectValue) {
		qisReinspectValueService.removeById(qisReinspectValue);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param list
	 * @return
	 */
	@AutoLog(value = "不良录入明细-批量删除")
	@ApiOperation(value="不良录入明细-批量删除", notes="不良录入明细-批量删除")
	//@RequiresPermissions("org.jeecg.modules.demo:qis_reinspect_value:deleteBatch")
	@RequestMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestBody List<QisReinspectValue> list) {
	    list.forEach(item->{
    			qisReinspectValueService.removeById(item);
    		});
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "不良录入明细-通过id查询")
	@ApiOperation(value="不良录入明细-通过id查询", notes="不良录入明细-通过id查询")
	@GetMapping(value = "/queryById")
	@DS("#header.his")
	public Result<QisReinspectValue> queryById(@RequestParam(name="id",required=true) String id) {
		QisReinspectValue qisReinspectValue = qisReinspectValueService.getById(id);
		if(qisReinspectValue==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(qisReinspectValue);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param qisReinspectValue
    */
    //@RequiresPermissions("org.jeecg.modules.demo:qis_reinspect_value:exportXls")
    @RequestMapping(value = "/exportXls")
    @DS("#header.his")
    public ModelAndView exportXls(HttpServletRequest request, QisReinspectValue qisReinspectValue,String sDate,
								  String eDate) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		QueryWrapper<QisReinspectValue> queryWrapper = QueryGenerator.initQueryWrapper(qisReinspectValue, request.getParameterMap());
		if (!StringUtils.isBlank(sDate)){
			queryWrapper.ge("CTIME",sDate);
		}
		String title = "";
		queryWrapper.le(!StringUtils.isBlank(eDate),"CTIME",eDate);
		Page<QisReinspectValue> page = new Page<QisReinspectValue>(1, 9999);
		IPage<QisReinspectValue> pageList = qisReinspectValueService.page(page, queryWrapper);
		// Step.3 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		//此处设置的filename无效 ,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.FILE_NAME, title);
		mv.addObject(NormalExcelConstants.CLASS, QisReinspectValue.class);
		//update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
		ExportParams exportParams = new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title);
		//update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
		mv.addObject(NormalExcelConstants.PARAMS, exportParams);
		mv.addObject(NormalExcelConstants.DATA_LIST, pageList.getRecords());
		return mv;
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("qis_reinspect_value:importExcel")
    @DS("#header.line")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, QisReinspectValue.class);
    }

}
